package com.budwk.app.sqfwV2.generator.povertyMonitoring;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelDataInto;
import com.budwk.app.sqfwV2.dto.entity.SqfwFamilyProperty;
import com.budwk.app.sqfwV2.dto.image.AuthenticRightConfigBo;
import com.budwk.app.sqfwV2.dto.povertyMonitoring.*;
import com.budwk.app.sqfwV2.enums.povertyMonitoring.PovertyMonitoringLabelEnum;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import jodd.util.ArraysUtil;
import org.apache.commons.lang3.StringUtils;
import org.nutz.lang.util.NutMap;


import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.addPictureToByteList;


public class FamilyAssetsStatisticalTableExcelGenerator extends BaseExcelGenerator {

    private String houseHoldId;
    private final String quarter;


    private String houseHoldNumber;

    private String templatePath;
    private List<Sqfw_formSummary_new> sqfwFormSummaryNews;

    private   List<SqfwFamilyProperty> sqfwFamilyPropertys;



    public void setSqfwFamilyProperty(   List<SqfwFamilyProperty> sqfwFamilyPropertys){
        this.sqfwFamilyPropertys =sqfwFamilyPropertys;
    }
   public void setSqfwFormSummaryNews( List<Sqfw_formSummary_new> sqfwFormSummaryNews){
       this.sqfwFormSummaryNews =sqfwFormSummaryNews;
   }
    public FamilyAssetsStatisticalTableExcelGenerator(String quarter){
        this.quarter=quarter;
    }

    @Override
    protected void customerPopularHeaderData(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

        List<Sqfw_formSummary_new> sqfwFormSummaryNews=this.sqfwFormSummaryNews;
        List<SqfwFamilyProperty> sqfwFamilyPropertys = this.sqfwFamilyPropertys;
        String collectorSignature = null;
        String farmerSignature= null;
        String whetherToHelpThePersonInCharge=null;
        List<Sqfw_formSummary_new> sort =  sqfwFormSummaryNews.stream().sorted(Comparator.comparing(Sqfw_formSummary_new::getFillDate,Comparator.reverseOrder())).collect(Collectors.toList());
        String fillDate= DateUtil.format(sort.get(0).getFillDate(), DatePattern.CHINESE_DATE_PATTERN);
        //公益性收入
        BigDecimal gyx=new BigDecimal(0);
        //其他工资性收益
        BigDecimal qt=new BigDecimal(0);
        //最定生活保障金
        BigDecimal  minLivAllow = new BigDecimal(0);
        //特困人员救助供养金
        BigDecimal  asstFund = new BigDecimal(0);
        //养老金或离退休金（含高龄津贴和补贴）
        BigDecimal  pension = new BigDecimal(0);
        //计划生育金
        BigDecimal  familyPlanAllowance = new BigDecimal(0);
        //生态补偿金
        BigDecimal  ecoComp = new BigDecimal(0);
        //产业奖励
        BigDecimal  propertyBonus = new BigDecimal(0);
        //就业奖励
        BigDecimal  workBonus = new BigDecimal(0);
        //其他转移性收入-残疾人补贴
        BigDecimal  disabilityAllowance = new BigDecimal(0);
        //其他转移性收入-直接补贴
        BigDecimal  farmAllowance = new BigDecimal(0);
        //其他转移性收入-赡养（抚养、扶养）收入
        BigDecimal  supportIncome = new BigDecimal(0);
        //其他
        BigDecimal  otherSubsidies = new BigDecimal(0);

        //转让土地经营权收入
        BigDecimal  D5 = new BigDecimal(0);
        //出租房屋收入
        BigDecimal  D6 = new BigDecimal(0);
        //出租设备和牲畜等动产收入
        BigDecimal  D7 = new BigDecimal(0);
        //确权到户的光伏收益
        BigDecimal  D8 = new BigDecimal(0);
        //存款利息等金融资产收益
        BigDecimal  D9 = new BigDecimal(0);
        //土地补偿费
        BigDecimal  D10 = new BigDecimal(0);
        //村级集体经济分红
        BigDecimal  D11 = new BigDecimal(0);
        //村级集体经济分红
        BigDecimal  Dtotle = new BigDecimal(0);



        //金额
       BigDecimal totle4 =  new BigDecimal(0);
       BigDecimal totle5 =  new BigDecimal(0);
       BigDecimal totle6 =  new BigDecimal(0);
       BigDecimal totle7 =  new BigDecimal(0);

        for (Sqfw_formSummary_new sqfwFormSummaryNew : sqfwFormSummaryNews) {
            PovertyAlleviationDTO povertyAlleviationDTO = JSONUtil.toBean(sqfwFormSummaryNew.getParamData(), PovertyAlleviationDTO.class);
            collectorSignature =  povertyAlleviationDTO.getCollectorSignature();
            farmerSignature = povertyAlleviationDTO.getFarmerSignature();
            whetherToHelpThePersonInCharge = povertyAlleviationDTO.getWhetherToHelpThePersonInCharge();
            this.houseHoldNumber =  povertyAlleviationDTO.getHouseHoldNumber();
            List<PersonWorkDTO> personWorkDTOS = povertyAlleviationDTO.getPersonWorkInfoList();
            //公益性收入和其他工资性收益
            for (PersonWorkDTO personWorkDTO : personWorkDTOS) {
                if(StrUtil.isEmpty(personWorkDTO.getGrossSalary())){
                    continue;
                }
                if(PovertyMonitoringLabelEnum.positionSituation_two.getCode().equals(personWorkDTO.getPositionSituation())){
                    gyx=NumberUtil.add(gyx, new BigDecimal( personWorkDTO.getGrossSalary()));
                }else if(PovertyMonitoringLabelEnum.positionSituation_one.getCode().equals(personWorkDTO.getPositionSituation())){
                    qt =NumberUtil.add(qt, new BigDecimal(personWorkDTO.getGrossSalary()));
                }
            }
            //转移性收入
            minLivAllow = NumberUtil.add(minLivAllow,povertyAlleviationDTO.getMinLivAllow());
            asstFund = NumberUtil.add(asstFund,povertyAlleviationDTO.getAsstFund());
            pension = NumberUtil.add(pension,povertyAlleviationDTO.getPension());
            familyPlanAllowance = NumberUtil.add(familyPlanAllowance,povertyAlleviationDTO.getFamilyPlanAllowance());
            ecoComp = NumberUtil.add(ecoComp,povertyAlleviationDTO.getEcoComp());
            propertyBonus = NumberUtil.add(propertyBonus,povertyAlleviationDTO.getPropertyBonus());
            workBonus = NumberUtil.add(workBonus,povertyAlleviationDTO.getWorkBonus());
            disabilityAllowance = NumberUtil.add(disabilityAllowance,povertyAlleviationDTO.getDisabilityAllowance());
            farmAllowance = NumberUtil.add(farmAllowance,povertyAlleviationDTO.getFarmAllowance());
            supportIncome = NumberUtil.add(supportIncome,povertyAlleviationDTO.getSupportIncome());
            otherSubsidies = NumberUtil.add(otherSubsidies,povertyAlleviationDTO.getOtherSubsidies());
            List<HouseholdIncomeAndPayTypeDTO> householdIncomeAndPayTypeDTOList = povertyAlleviationDTO.getHouseholdIncomeAndPayTypeDTOList();
            if(CollectionUtil.isNotEmpty(householdIncomeAndPayTypeDTOList)){
                for (HouseholdIncomeAndPayTypeDTO householdIncomeAndPayTypeDTO : householdIncomeAndPayTypeDTOList) {
                    if("1".equals(householdIncomeAndPayTypeDTO.getType())){
                        //财产性收入
                        if("1".equals(householdIncomeAndPayTypeDTO.getCategoryType())){
                            D5 = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList().get(0).getTotalPrice();
                        }
                        if("2".equals(householdIncomeAndPayTypeDTO.getCategoryType())){
                            D6 = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList().get(0).getTotalPrice();
                        }
                        if("3".equals(householdIncomeAndPayTypeDTO.getCategoryType())){
                            D7 = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList().get(0).getTotalPrice();
                        }
                        if("4".equals(householdIncomeAndPayTypeDTO.getCategoryType())){
                            D8 = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList().get(0).getTotalPrice();
                        }
                        if("5".equals(householdIncomeAndPayTypeDTO.getCategoryType())){
                            D9 = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList().get(0).getTotalPrice();
                        }
                        if("6".equals(householdIncomeAndPayTypeDTO.getCategoryType())){
                            D10 = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList().get(0).getTotalPrice();
                        }
                        if("7".equals(householdIncomeAndPayTypeDTO.getCategoryType())){
                            D11 = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList().get(0).getTotalPrice();
                        }

                    }
                }
            }
        }
        int C = 12;
        int H=5;
        int ZCZY=5;
        int SCXZC=5;
        int JSBT=5;
        if(CollectionUtil.isNotEmpty(sqfwFamilyPropertys)){
            for (SqfwFamilyProperty sqfwFamilyProperty : sqfwFamilyPropertys) {
                FamilyPropertyParamDataDTO familyPropertyParamData =   JSONUtil.toBean(sqfwFamilyProperty.getParamData(), FamilyPropertyParamDataDTO.class);
                List<HouseholdIncomeAndPayTypeDTO> householdIncomeAndPayTypeDTOList = familyPropertyParamData.getHouseholdIncomeAndPayTypeDTOList();
                if(CollectionUtil.isNotEmpty(householdIncomeAndPayTypeDTOList)){
                    for (HouseholdIncomeAndPayTypeDTO householdIncomeAndPayTypeDTO : householdIncomeAndPayTypeDTOList) {
                        if("0".equals(householdIncomeAndPayTypeDTO.getCategoryType())){
                            List<HouseholdIncomeAndPayDetailDTO> incomeAndPayDetailDTOS = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList();
                            for (HouseholdIncomeAndPayDetailDTO incomeAndPayDetailDTO : incomeAndPayDetailDTOS) {
                                //其他
                                excelParam.put("C"+C,incomeAndPayDetailDTO.getName());
                                excelParam.put("D"+C,incomeAndPayDetailDTO.getTotalPrice());
                                Dtotle = NumberUtil.add(Dtotle,incomeAndPayDetailDTO.getTotalPrice()) ;
                                C++;
                            }
                        }
                        //生产经营性收入
                        if("3".equals(householdIncomeAndPayTypeDTO.getType())){
                            List<HouseholdIncomeAndPayDetailDTO> incomeAndPayDetailDTOS = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList();
                            for (HouseholdIncomeAndPayDetailDTO incomeAndPayDetailDTO : incomeAndPayDetailDTOS) {
                                //变现
                                if("2".equals(incomeAndPayDetailDTO.getType())){
                                    excelParam.put("H"+H,incomeAndPayDetailDTO.getName());
                                    excelParam.put("HCL"+H,incomeAndPayDetailDTO.getThroughput());
                                    excelParam.put("HDY"+H,incomeAndPayDetailDTO.getPrice());
                                    excelParam.put("HJE"+H,incomeAndPayDetailDTO.getTotalPrice());
                                    totle4=NumberUtil.add(totle4,incomeAndPayDetailDTO.getTotalPrice());
                                    H++;
                                }
                                //资产自用
                                if("3".equals(incomeAndPayDetailDTO.getType())){
                                    excelParam.put("ZCZY"+ZCZY,incomeAndPayDetailDTO.getName());
                                    excelParam.put("ZCZYCL"+ZCZY,incomeAndPayDetailDTO.getThroughput());
                                    excelParam.put("ZCZYDY"+ZCZY,incomeAndPayDetailDTO.getPrice());
                                    excelParam.put("ZCZYJE"+ZCZY,incomeAndPayDetailDTO.getTotalPrice());
                                    totle5=NumberUtil.add(totle5,incomeAndPayDetailDTO.getTotalPrice());
                                    ZCZY++;
                                }
                            }
                        }
                        //生产经营性支出
                        if("2".equals(householdIncomeAndPayTypeDTO.getType())){
                            List<HouseholdIncomeAndPayDetailDTO> incomeAndPayDetailDTOS = householdIncomeAndPayTypeDTO.getHouseholdIncomeAndPayDetailDTOList();
                            for (HouseholdIncomeAndPayDetailDTO incomeAndPayDetailDTO : incomeAndPayDetailDTOS) {
                                //生产性支出
                                if("4".equals(incomeAndPayDetailDTO.getType())){
                                    excelParam.put("SCXZC"+SCXZC,incomeAndPayDetailDTO.getName());
                                    excelParam.put("SCXZCJE"+SCXZC,incomeAndPayDetailDTO.getTotalPrice());
                                    totle6=NumberUtil.add(totle6,incomeAndPayDetailDTO.getTotalPrice());
                                    SCXZC++;
                                }
                                //减少补贴
                                if("5".equals(incomeAndPayDetailDTO.getType())){
                                    excelParam.put("JSBT"+JSBT,incomeAndPayDetailDTO.getName());
                                    excelParam.put("JSBTJE"+JSBT,incomeAndPayDetailDTO.getTotalPrice());
                                    totle7=NumberUtil.add(totle7,incomeAndPayDetailDTO.getTotalPrice());
                                    JSBT++;
                                }
                            }
                        }

                    }
                }
            }
            }
        int row = 16;
        int max = NumberUtil.max(new int[]{C,H,ZCZY,SCXZC,JSBT});
        if(max<=row){
            max=row;
            setTemplatePath("sqfwV2/povertyMonitoring/jtzc.xlsx");
        }else {
            if(max>15&&max<=21){
                setTemplatePath("sqfwV2/povertyMonitoring/jtzc16.xlsx");
            }
            if(max>20&&max<=26){
                setTemplatePath("sqfwV2/povertyMonitoring/jtzc22.xlsx");
            }
            if(max>26&&max<=31){
                setTemplatePath("sqfwV2/povertyMonitoring/jtzc23.xlsx");
            }
        }
        if(H<max){
            for (int i=max;i>=H;i--){
                excelParam.put("H"+i,"-");
                excelParam.put("HCL"+i,"-");
                excelParam.put("HDY"+i,"-");
                excelParam.put("HJE"+i,"-");
            }
        }
        if(ZCZY<max){
            for (int i=max;i>=ZCZY;i--){
                excelParam.put("ZCZY"+i,"-");
                excelParam.put("ZCZYCL"+i,"-");
                excelParam.put("ZCZYDY"+i,"-");
                excelParam.put("ZCZYJE"+i,"-");
            }
        }
        if(SCXZC<max){
            for (int i=max;i>=SCXZC;i--){
                excelParam.put("SCXZC"+i,"-");
                excelParam.put("SCXZCJE"+i,"-");
            }
        }

        if (JSBT < max) {
            for (int i=max;i>=JSBT;i--){
                excelParam.put("JSBT"+i,"-");
                excelParam.put("JSBTJE"+i,"-");
            }
        }
        if (C < max) {
            for (int i=max;i>=C;i--){
                excelParam.put("C"+i,"-");
                excelParam.put("D"+i,"-");
            }
        }
        excelParam.put("gyx",gyx);
        excelParam.put("qt",qt);
        excelParam.put("totle1",NumberUtil.add(gyx,qt));

        excelParam.put("D5",D5);
        excelParam.put("D6",D6);
        excelParam.put("D7",D7);
        excelParam.put("D8",D8);
        excelParam.put("D9",D9);
        excelParam.put("D10",D10);
        excelParam.put("D11",D11);
        excelParam.put("totle2",NumberUtil.add(D5,D6,D7,D8,D9,D10,D11,Dtotle));

        excelParam.put("minLivAllow",minLivAllow);
        excelParam.put("asstFund",asstFund);
        excelParam.put("pension",pension);
        excelParam.put("familyPlanAllowance",familyPlanAllowance);
        excelParam.put("propertyBonus",propertyBonus);
        excelParam.put("workBonus",workBonus);
        excelParam.put("ecoComp",ecoComp);
        excelParam.put("disabilityAllowance",disabilityAllowance);
        excelParam.put("farmAllowance",farmAllowance);
        excelParam.put("supportIncome",supportIncome);
        excelParam.put("otherSubsidies",otherSubsidies);
        excelParam.put("totle3",NumberUtil.add(minLivAllow,asstFund,pension,familyPlanAllowance,ecoComp,propertyBonus,workBonus,disabilityAllowance,farmAllowance,supportIncome,otherSubsidies));
        excelParam.put("totle4",NumberUtil.add(totle4,totle5));
        excelParam.put("totle5",NumberUtil.sub(totle6,totle7));
        excelParam.put("houseHoldNumber",houseHoldNumber);
        excelParam.put("quarter",quarter);
        excelParam.put("totle6",NumberUtil.sub(NumberUtil.add((BigDecimal) excelParam.get("totle1"),(BigDecimal) excelParam.get("totle2"),(BigDecimal) excelParam.get("totle3"),(BigDecimal) excelParam.get("totle4")),(BigDecimal) excelParam.get("totle5")));
        excelParam.put("fillDate",fillDate);
        //采集人签字
        excelParam.put("huifangVisitPictures", addPictureToByteList(collectorSignature, 1));
        //农户签字
        excelParam.put("checkedUnitLeader", addPictureToByteList(farmerSignature, 1));

        if(whetherToHelpThePersonInCharge.equals("是")){
            excelParam.put("Y", "是☑ 否□");
        }else {
            excelParam.put("Y", "是□ 否☑");
        }

    }

    @Override
    protected void customerPopularHeaderDataForAdmin(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {


    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeErgodic(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected boolean addRowDataCondition(ExcelRowDataParam rowDataParam, ExcelGenerateParam excelGenerateParam) {
        return false;
    }

    @Override
    protected void customerPopularSheetData(List<Map<String, Object>> listMap, ExcelGenerateParam excelGenerateParam) {



    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected void customerPopularRowDataMapForAdmin(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeCreate(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected String getTemplatePath(ExcelGenerateParam excelGenerateParam) {
        return this.templatePath;
    }



    protected String setTemplatePath(String templatePath) {
        return this.templatePath=templatePath;
    }

    @Override
    protected String getDownloadFileName(ExcelGenerateParam excelGenerateParam) {
        return this.houseHoldNumber;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return AuthenticRightConfigBo.class;

    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParamBO) {

    }
}
